package org.edu.eece262.taskshare;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class ContactsDBAdapter {
	
	// Let's be a Singleton
	private static ContactsDBAdapter instance = null;

	// Member Variables
	private static String DB_NAME = "TaskShareContacts.db";
	private static final String DB_TABLE = "Contacts";
	private static final int DB_VERSION = 20;

	public static final String DB_KEY_ID = "_id";

	// Public Key (Column) Names for convenience
	public static final String KEY_USER_NAME = "Username";
	public static final String KEY_NAME = "Name";
	public static final String KEY_EMAIL = "Email";
	public static final String KEY_PHONE = "Phone";
	public static final String KEY_STATUS = "Status";

	// Public Column numbers for convenience
	public static final int USER_NAME_COLUMN = 1;
	public static final int NAME_COLUMN = 2;
	public static final int EMAIL_COLUMN = 3;
	public static final int PHONE_COLUMN = 4;
	public static final int STATUS_COLUMN = 5;
	
    public static class StatusType {
    	public static final int ACTIVE = 0;
    	public static final int REQUESTED = 1;
    	public static final int WAITING = 2; 
    	public static final int ACCEPTED = 3;
    	public static final int DELETED = 4;
    }

	private static final String DB_CREATE = "create table " + DB_TABLE + " ("
			+ DB_KEY_ID + " integer primary key autoincrement, "
			+ KEY_USER_NAME + " text, " + KEY_NAME + " text, " + KEY_EMAIL
			+ " text, " + KEY_PHONE + " text, " + KEY_STATUS + " int);";

	private SQLiteDatabase db;

	private final Context context;
	private DBHelper dbHelper;

	public static final String PREFS_NAME = "TASKSHARELOGIN";
	
	public static ContactsDBAdapter getContactsDBAdapter(Context _context) {
		SharedPreferences settings = _context.getSharedPreferences(PREFS_NAME, 0);
		if(instance == null || (DB_NAME.equals(settings.getString("user", "") + "TaskShareContacts.db"))) {
			DB_NAME = settings.getString("user", "") + "TaskShareContacts.db";
			instance = new ContactsDBAdapter(_context);
			return instance;
		} else {
			return instance;
		}
	}
	
	private ContactsDBAdapter(Context _context) {
		context = _context;
		dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
	}

	public ContactsDBAdapter open() throws SQLException {
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	public long insertUser(String username, String name, String email, String phone, int status) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_USER_NAME, username);
		contentValues.put(KEY_NAME, name);
		contentValues.put(KEY_EMAIL, email);
		contentValues.put(KEY_PHONE, phone);
		contentValues.put(KEY_STATUS, status);
		return db.insert(DB_TABLE, null, contentValues);
	}

	public boolean removeEntry(long _rowIndex) {
		return db.delete(DB_TABLE, DB_KEY_ID + "=" + _rowIndex, null) > 0;
	}

	public Cursor getAllEntries() {
		return db.query(DB_TABLE, null, null, null, null, null, null);
	}
	
	public Cursor getContacts() {
		return db.query(DB_TABLE, null, KEY_STATUS + " = " + StatusType.ACTIVE + "", null, null, null, null);
	}
	
	public Cursor getPendingContacts() {
		return db.query(DB_TABLE, null, KEY_STATUS + " = " + StatusType.WAITING + "", null, null, null, null);
	}
	
	public Cursor getContactRequests() {
		return db.query(DB_TABLE, null, KEY_STATUS + " = " + StatusType.REQUESTED + "", null, null, null, null);
	}
	
	public Cursor getEntriesMatching(String username) {
		return db.query(DB_TABLE, null, KEY_USER_NAME + " = '" + username + "'", null, null, null, null);
	}

	public int updateEntry(int key, String username, String name, String email, String phone, int status) {
		String where = DB_KEY_ID + "=" + key;
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_USER_NAME, username);
		contentValues.put(KEY_NAME, name);
		contentValues.put(KEY_EMAIL, email);
		contentValues.put(KEY_PHONE, phone);
		contentValues.put(KEY_STATUS, status);
		return db.update(DB_TABLE, contentValues, where, null);
	}
	
	public int updateStatus(int key, int status) {
		String where = DB_KEY_ID + "=" + key;
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_STATUS, status);
		return db.update(DB_TABLE, contentValues, where, null);
	}

	private static class DBHelper extends SQLiteOpenHelper {
		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		// Called when no database exists in
		// disk and the helper class needs
		// to create a new one.
		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(DB_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {
			// Log the version upgrade.
			Log.w("ContactsDBAdapter", "Upgrading from version " + _oldVersion
					+ " to " + _newVersion
					+ ", which will destroy all old data");

			_db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);

			onCreate(_db);
		}
	}
}
